package vodoo.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import vodoo.gui.video.VideoControlPanel;
import vodoo.gui.video.VideoPanel;

public class SplittedVideoPanel extends JPanel {
	//--------------------------------------------------------- CONSTANTES --//
	//---------------------------------------------------------- ATTRIBUTS --//
	
	private List<WrapperPanel> panels;
	
	//------------------------------------------------------ CONSTRUCTEURS --//
	
	public SplittedVideoPanel() {
		panels = Collections.synchronizedList(new ArrayList<WrapperPanel>());
		
		setBorder(new EmptyBorder(5, 5, 5, 5));
		setBackground(new Color(0x1e2635));
	}
	
	//----------------------------------------------------------- METHODES --//
	
	public boolean isFull() {
		return panels.size() == 9;
	}
	
	public VideoControlPanel[] getVideoPanels() {
		synchronized (panels) {
			VideoControlPanel[] videos = new VideoControlPanel[panels.size()];
			for (int i = 0; i < videos.length; i++)
				videos[i] = panels.get(i).getVideoPanel();
			return videos;
		}
	}
	
	public void addVideoPanel(VideoControlPanel panel) {
		synchronized (panels) {
			if (panels.size() == 9)
				return;
			
			panels.add(new WrapperPanel(panel));
			rebuild();
		}
	}
	
	public void removeVideoPanel(VideoControlPanel panel) {
		synchronized (panels) {
			for (Iterator<WrapperPanel> i = panels.iterator(); i.hasNext();) {
				VideoPanel p = i.next().getVideoPanel();
				
				if (p == panel) {
					i.remove();
					rebuild();
					break;
				}
			}
		}
	}
	
	//--------------------------------------------------- METHODES PRIVEES --//
	
	private void rebuild() {
		int count = panels.size();
		
		if (count == 0) {
			removeAll();
		} else {
			int rows = (int) Math.ceil(Math.sqrt(count));
			int cols = (int) Math.ceil(count / (float) rows);
			
			removeAll();
			setLayout(new GridLayout(cols, rows));
			
			for (int i = 0; i < cols * rows; i++)
				if (i < panels.size())
					add(panels.get(i));
		}
		
		repaint();
		super.revalidate();
	}

	//---------------------------------------------------- CLASSES PRIVEES --//
	
	private class WrapperPanel extends JPanel {
		//----------------------------------------------------- CONSTANTES --//
		//------------------------------------------------------ ATTRIBUTS --//
		
		private VideoControlPanel videoPanel;
		
		//-------------------------------------------------- CONSTRUCTEURS --//
		
		public WrapperPanel(VideoControlPanel video) {
			this.videoPanel = video;
			
			setLayout(new BorderLayout());
			setBorder(new EmptyBorder(5, 5, 5, 5));
			setOpaque(false);
			
			add(videoPanel, BorderLayout.CENTER);
		}
		
		//------------------------------------------------------- METHODES --//
		
		public VideoControlPanel getVideoPanel() {
			return videoPanel;
		}
		
		//----------------------------------------------- METHODES PRIVEES --//
	}
}
